<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/8/24
 * Time: 17:03
 */

namespace app\agent\model\user;

use app\agent\model\BaseModel;

class AgentUser extends BaseModel
{
    protected $name = 'agent_user';

    /**
     * 获取代言人列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        return $this->where('is_delete', '0')
            ->order(['create_time' => 'desc'])
            ->paginate(15);
    }

    public function add($data)
    {
        if (empty($data['user_name'])) {
            $this->error = '用户名不能为空';
            return false;
        }

        if (empty($data['password'])) {
            $this->error = '用户密码不能为空';
            return false;
        }

        try {
            $data['password'] = yoshop_hash($data['password']);
            $this->allowField(true)->save($data);
            $this->add($data);
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

}